package com.hframe.http.servlet;

import com.sun.istack.internal.NotNull;

/**
 * 逻辑处理类
 * Created by heqian on 17-9-25.
 */
public abstract class HttpLogic {
    protected HttpSkipServletResponse response;
    protected HttpSkipServletRequest request;

    /**
     * 开始处理逻辑
     * @param request http请求
     * @param response http响应
     * @return
     */
    public boolean logic(@NotNull HttpSkipServletRequest request, @NotNull HttpSkipServletResponse response) {
        this.request = request;
        this.response = response;
        boolean ret = false;
        boolean isError = true;
        try {
            if (onInit())
                ret = onLogic();
            isError = false;
        } catch (Exception e) {
            onError(e);
        } finally {
            onFinish(ret, isError);
        }
        return ret;
    }

    /**
     * 处理逻辑有错误回调
     * @param e
     */
    protected abstract void onError(Exception e);

    /**
     * 逻辑处理开始回调
     * @return
     * @throws Exception
     */
    protected abstract boolean onInit() throws Exception;

    /**
     * 逻辑处理回调
     * @return
     * @throws Exception
     */
    protected abstract boolean onLogic() throws Exception;

    /**
     * 逻辑处理完成回调
     * @param isSkip 是否有页面跳转
     * @param isError 是否有错误
     */
    protected abstract void onFinish(boolean isSkip, boolean isError);

}
